const mongoose = require('mongoose');

async function main (){ 
    await mongoose.connect('mongodb://localhost:27017/cat');


    let kittySchema = new mongoose.Schema({ //定义结构
        name: String,
        age:Number,
        time:Date,
        description:String
    })
    
    kittySchema.methods.speak = function speak() {
        const greeting = this.name
            ? 'Meow name is ' + this.name
            : 'I don\'t have a name';
        console.log(greeting);
    };

    const KittyCat = mongoose.model('KittyCat',kittySchema)

    let cat1 = new KittyCat({name:'c1', age:3, time:new Date(), description:'这是一只19岁的小猫咪'})
    cat1.save().then(res=>{
        console.log('success cat2')
    })
    cat1.speak()

    let cat2 = new KittyCat({name:'c2', age:3, time:new Date(), description:'这是一只3岁的猫咪'})
    // console.log(cat2,'===cat2')
    cat2.speak()
    cat2.save().then(res=>{
        console.log('success cat2')
    })
    KittyCat.find().then(res=>{   // 查询当前模型对应的所有数据
        // console.log(res,'=====reskitty')
    })

    KittyCat.find({name:/^c/}).then(res=>{
        console.log(res,'===find')
    })

    KittyCat.findOne({name:'c1'}).then(res=>{
        console.log(res,'====findone')
    })
    
}

main().catch(err=>{
    console.log(err,'====err')
})
